#coding=utf-8
import arcpy
path = r'D:\WorkData\20250712四川河流\0819处理'
arcpy.env.workspace = path
arcpy.env.overwriteOutput = True
spatial_ref = arcpy.SpatialReference(4326)
riv = r'D:\WorkData\20250712四川河流\0819处理\dis_n_0.shp'.decode('utf-8')
riv2 = r'D:\WorkData\20250712四川河流\0819处理\rivs1.shp'.decode('utf-8')

cs1 = arcpy.da.UpdateCursor(riv,['SHAPE@','name','flowRiv','flowId','lpDisRiv'])
cs2 = arcpy.da.SearchCursor(riv2,['SHAPE@','name','rivId','level'])
i = 0
for row1 in cs1:
    disList = []
    for row2 in cs2:
        lp = arcpy.PointGeometry(row1[0].lastPoint,spatial_ref)
        dis = lp.distanceTo(row2[0])
        flowRiv = row2[1]
        flowId = row2[2]
        res = [dis,flowRiv,flowId]
        if row2[3] != '0':
            disList.append(res)
    cs2.reset()
    disList.sort(key=lambda x: x[0])
    print i, row1[1], '流入', disList[0][1], disList[0][0], disList[0][2]
    row1[4] = disList[0][0]
    row1[2] = disList[0][1]
    row1[3] = disList[0][2]
    cs1.updateRow(row1)
    i += 1